package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PricingPlanServInfoLinkageAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AttrHandler(PricingPlanServInfoLinkageAttrHandler.class)
public class PricingPlanServInfoLinkageAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String fieldName = attr.getFieldName();
        KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
        if(KiteObjectUtils.isEmpty(prodInst)){
            return;
        }
        List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
        if(KiteListUtils.isNotEmpty(funcs) && KiteStringUtils.equals("200004183",fieldName)){
            //账户费用（元/期限）
            String acctFee = attr.getValue();
            boolean isCid = false;
            for(KiteProdInstFunc func : funcs){
                //通信套包类型
                if(!KiteStringUtils.equals(inst.getInstId(),func.getInstId())){
                    KiteAttr acctFeeAttr = func.getAttr("200004183");
                    if(!KiteObjectUtils.isEmpty(acctFeeAttr)){
                        if(KiteStringUtils.isEmpty(acctFee)){
                            acctFee = acctFeeAttr.getValue();
                        }else{
                            acctFee = StrTools.numAdd(acctFee,acctFeeAttr.getValue());
                        }
                    }
                }
                KiteAttr cidAttr = func.getAttr("200003693");
                if(!KiteObjectUtils.isEmpty(cidAttr)){
                    String cidValue = cidAttr.getValue();
                    if(KiteStringUtils.equals("100006426",cidValue)){
                        isCid = true;
                    }
                }
            }
            if(isCid){
                acctFee = StrTools.numAdd(acctFee,"5");
            }
            for(KiteProdInstFunc func : funcs){
                //0-∞万户数量档最终账户费(元)设置默认值
                KiteAttr kiteAttr1 = func.getAttr("200004184");
                if(!KiteObjectUtils.isEmpty(kiteAttr1)){
                    operateInstAttrService.setAttrValue(func, "200004184", acctFee, data.getSceneInstId(), null);
                    operateInstAttrService.setAttrValue(func, "200001694", "100", data.getSceneInstId(), null);
                }
                //0-1万户数量档最终账户费(元)设置默认值
                KiteAttr kiteAttr2 = func.getAttr("200004185");
                if(!KiteObjectUtils.isEmpty(kiteAttr2)){
                    operateInstAttrService.setAttrValue(func, "200004185", acctFee, data.getSceneInstId(), null);
                }
            }
        }
    }


    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
